package com.lagou.module4hw.hw56.client;

import com.lagou.module4hw.hw56.parsepackage.ParsePack;
import com.lagou.module4hw.hw56.server.Server;

import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;

public class ClientReceive implements Runnable {

    //用户名
    private String name;

    //用户dir
    private String userDir;

    //当前sockt
    private Socket socket;

    //构造方法
    public ClientReceive(String name, Socket socket) {
        this.name = name;
        this.socket = socket;
    }

    @Override
    public void run() {


            BufferedInputStream bis = null;
            try {
                bis = new BufferedInputStream(socket.getInputStream());

                    while (true) {
                        //先读取包头
                       /* synchronized (socket) {
                            try {
                                socket.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }*/

                            byte[] header = new byte[9];
                            if (-1 == bis.read(header)) {
                                //System.out.println("服务已下线");
                                continue;
                            }
                            //第一包数据包的总大小
                            long firstPackSize = ParsePack.getPackSize(header);

                            if (ParsePack.isString(header)) {
                                //普通字符串
                                byte[] data = new byte[(int) firstPackSize];
                                bis.read(data);
                                String mesg = new String(data);
                                System.out.println();
                                System.out.println(mesg);
                                System.out.print(name + " --> : ");

                            } else {
                                //是文件，第一个包是文件头，先接收文件头，后接收文件内容
                                byte[] fileNameBytes = new byte[(int) firstPackSize];
                                //读取文件头第一包 1 + 8(文件名大小)+ 文件名 + 8(文件大小)
                                bis.read(fileNameBytes);
                                String fileName = new String(fileNameBytes);
                                //System.out.println("文件名是: " + fileName);

                                //再读8个字节是文件大小
                                byte[] fileSizeBytes = new byte[8];
                                bis.read(fileSizeBytes);
                                long fileSize = ByteBuffer.wrap(fileSizeBytes).getLong();
                                //System.out.println("文件的大小是: " + fileSize);

                                //开始读取具体文件内容
                                //创建文件输出流保存文件信息
                                setUserDir();
                                BufferedOutputStream bos = new BufferedOutputStream(
                                        new FileOutputStream(getUserDir() + "/" + fileName));
                                //总共需要读取fileSize字节 一次读取1024个字节
                                byte[] dataBytes = new byte[1024];
                                long leftSize = fileSize;
                                long readSize = 0;
                                while (leftSize > 0) {
                                    //读取readSize字节放到dataBytes中
                                    readSize = bis.read(dataBytes);
                                    //把读到的字节写入文件中
                                    bos.write(dataBytes, 0, (int) readSize);
                                    //文件总大小-当前读取的大小 = 还剩要读取的大小
                                    leftSize -= readSize;
                                }
                                bos.flush();
                                System.out.println("\t\t接收到一个文件:" + getUserDir() + "\\" + fileName);
                                System.out.print(name + " --> : ");
                            }
                        //}
                    }
            } catch (IOException e) {
                //e.printStackTrace();
                System.out.println("客户端下线了");
            }
    }

    private String getUserDir() {
        return this.userDir;
    }

    private void setUserDir() {
        userDir = "D:\\"+"client\\" +name;
        File dir = new File(userDir);
        if(!dir.exists()){
            System.out.println("用户目录不存在，开始创建目录");
            //创建文件
            dir.mkdirs();
        }
    }
}
